package com.momoauth.common.tps.aliyun.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AliPayUtil {

    private static final String timeout_express = "2m";
    private static final String charset = "UTF-8";

    /**
     * 支付宝手机网站支付
     * @param alipay_client
     * @param alipay_request
     * @param response
     * @param out_trade_no
     * @param total_amount
     * @param product_code
     */
    public static void alipay(
            AlipayClient alipay_client,
            AlipayTradeWapPayRequest alipay_request,
            HttpServletResponse response,
            String out_trade_no,
            String total_amount,
            String product_code,
            String subject,
            String body
    ) {
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);

        String form = "";
        try {
            form = alipay_client.pageExecute(alipay_request).getBody();
            response.setContentType("text/html;charset=" + charset);
            response.getWriter().write(form);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException | IOException e) {
            e.printStackTrace();
        }
    }
}
